﻿  CREATE PROCEDURE [dbo].[usp_caratulaPresentacion_obtenerCerradasXOS]
@ObraSocialXPlanId as int,
@MesId as int,
@AnoId as int

AS


DELETE FROM TMPCaratulas
	INSERT INTO TMPCaratulas
	SELECT P.PresentacionId, F.FarmaciaNombre, CP.CaratulaPresentacionRecetas as RecetasOriginal, CP.CaratulaPresentacionTotalFacturado AS TotalOriginal, CP.CaratulaPresentacionTotalCobrar AS ACargoOSOriginal, 0 as RecetasIntermedia, 0.00 as TotalIntermedia, 0.00 as ACargoOSIntermedia
	FROM CaratulaPresentacion CP, Presentacion P, Farmacia F 
	WHERE P.ObraSocialXPlanId = @ObraSocialXPlanId And P.PresentacionAno = @AnoId
		And P.MesId = @MesId And P.EstadoPresentacionId = 2 
		And P.PresentacionId = CP.PresentacionId And F.FarmaciaId = P.FarmaciaId
		AND CP.TipoCaratulaId = 1

	UPDATE TMPCaratulas
	SET RecetasIntermedia = CP.CaratulaPresentacionRecetas, TotalIntermedia = CP.CaratulaPresentacionTotalFacturado, ACargoOSIntermedia = CP.CaratulaPresentacionTotalCobrar
	FROM CaratulaPresentacion CP
	WHERE CP.PresentacionId = TMPCaratulas.PresentacionId
	AND	CP.TipoCaratulaId = 2

select* from TMPCaratulas
		
		